import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Index',
      redirect:'/good-detail',
      component:()=>import('@/views/index'),
      meta:{
      	title:'首页'
      }
    },
    {
      path: '/good-detail',
      name: 'GoodDetail',
      component:()=>import('@/views/good-detail'),
      meta:{
        title:'商品详情'
      }
    },
    {
      path: '/good-order',
      name: 'GoodOrder',
      component:()=>import('@/views/good-order'),
      meta:{
        title:'商品订单'
      }
    },
    {
      path: '/good-buy',
      name: 'GoodBuy',
      component:()=>import('@/views/good-buy'),
      meta:{
        title:'立即购买'
      },
      props:true
    }
  ]
})

router.beforeEach((to, from, next) => {
  let title = to.meta.title
  if(title) {
    document.title = title
    // 下面是关键的地方，匹配苹果设备
    if(/ip(hone|od|ad)/i.test(navigator.userAgent)) {
      let i = document.createElement('iframe')
      i.src = "//m.baidu.com/favicon.ico" // src要设置成一个实际存在的图片，最好是比较小的，如果不存在，在使用this.$router.go(-1)这种跳转时，会出现错误
      i.style.display = "none"
      i.onload = () => {
        setTimeout(() => {
          document.body.removeChild(i)
        },0)
      }
    }
  }
  next()
})

export default router
